<?php
/**
 * 文章页自定义字段面板 
 * @package WordPress
 * @subpackage wheatv
 */

$post_meta_boxes =
array(	
        "author" => array(
	"name" => "author",
	"types" => "text",
	"title" => "作者/出处:"),
);


function wheatv_post_meta_boxes() {
    global $post, $post_meta_boxes;
    foreach($post_meta_boxes as $meta_box) {
        $meta_box_value = get_post_meta($post->ID, $meta_box['name'].'_value', true);
        if($meta_box_value == "")
            $meta_box_value = $meta_box['std'];
        echo'<input type="hidden" name="'.$meta_box['name'].'_noncename" id="'.$meta_box['name'].'_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';
        echo'<h4>'.$meta_box['title'].'</h4>';
		switch($meta_box['types']){
		  case 'textarea':
            echo '<textarea cols="80" rows="3" name="'.$meta_box['name'].'_value" style="width:90%;height:100px;">'.$meta_box_value.'</textarea><br />';
			break;
		  case 'text':
		    echo '<input type="text" name="'.$meta_box['name'].'_value" value="'.$meta_box_value.'" style="width:90%" /><br />';
			break;
	     case 'select':
		    echo '<select name="'.$meta_box['name'].'_value">';
			echo '<option value="" '.$select.'>选择该文章所属周刊</option>';
			if(!empty($meta_box['options']))
			{
				foreach ($meta_box['options'] as $option)
				{
					 $select = '';
				     if($meta_box_value==$option) {
					  $select = 'selected=selected"';
				     }
					echo '<option value="'.$option.'" '.$select.'>'.$option.'</option>';
				}
			}
			echo'</select>';
			break;
		 case 'checkbox':
		    foreach ($meta_box['options'] as $option){
				$checkyes = '';
				if(strpos($meta_box_value, $option) !== false) {
					$checkyes = 'checked="checked"';
				}
		       echo'<input type="checkbox" name="'.$meta_box['name'].'_value[]" value="'.$option.'" '.$checkyes.' />'.$option.'&nbsp;&nbsp;';
			}
			break;
		 case 'radio':
		    foreach ($meta_box['options'] as $option){
		  ?>
		       <input type="radio" name="<?php echo $meta_box['name']?>_value" value="<?php echo $option;?>" <?php if ($option==$meta_box_value) { echo ' checked="checked"'; } ?> /> <?php echo $option; ?> &nbsp;&nbsp;
		  <?php	
            }
			break;
		}
    }
}

function wheatv_post_create_meta_box() {
    global $theme_name;

    if ( function_exists('add_meta_box') ) {
        add_meta_box( 'post-meta-boxes', '文章设置', 'wheatv_post_meta_boxes', 'post', 'side', 'high' );
    }
}

function wheatv_post_save_postdata( $post_id ) {
    global $post, $post_meta_boxes;
    foreach($post_meta_boxes as $meta_box) {
        if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], plugin_basename(__FILE__) ))  {
            return $post_id;
        }

        if ( 'page' == $_POST['post_type'] ) {
            if ( !current_user_can( 'edit_page', $post_id ))
                return $post_id;
        } 

        else {
            if ( !current_user_can( 'edit_post', $post_id ))
                return $post_id;
        }

        //$data = $_POST[$meta_box['name'].'_value'];
		if ($meta_box['types'] == 'checkbox' && is_array($_POST[$meta_box['name'].'_value'])) {
			$data = implode(",",$_POST[$meta_box['name'].'_value']);
		}else {
			$data = $_POST[$meta_box['name'].'_value'];
		}

        if(get_post_meta($post_id, $meta_box['name'].'_value') == "")
            add_post_meta($post_id, $meta_box['name'].'_value', $data, true);
        elseif($data != get_post_meta($post_id, $meta_box['name'].'_value', true))
            update_post_meta($post_id, $meta_box['name'].'_value', $data);
        elseif($data == "")
            delete_post_meta($post_id, $meta_box['name'].'_value', get_post_meta($post_id, $meta_box['name'].'_value', true));
    }
}

add_action('admin_menu', 'wheatv_post_create_meta_box');
add_action('save_post', 'wheatv_post_save_postdata');
?>